package com.eeds.controller;

import com.eeds.para.ParametersUtil;
import org.apache.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统页面视图
 * 
 * @author Flying
 * @email flying_miku@sina.com
 * @date 2016年11月24日 下午11:05:27
 */
@Controller
public class SysPageController {
	
	@RequestMapping(value="modules/{module}/{url}.html",method=RequestMethod.GET)
	public ModelAndView module(@PathVariable("module") String module, @PathVariable("url") String url, @RequestParam(value = "id",required = false) String id){
		Map<String,Object> data = new HashMap<>(16);
		if(ParametersUtil.isNotNull(id)){
			data.put("id",id);
		}
		return new ModelAndView("modules/" + module + "/" + url , data);
	}

 	@RequestMapping(value="modules/**/{url}.html",method=RequestMethod.GET)
	public ModelAndView modules(@PathVariable("url") String url,
								@RequestParam(value = "id",required = false) String id,
								@RequestParam(value = "subsetId",required = false) String subsetId,
								@RequestParam(value = "type",required = false) String type,
								HttpServletRequest request){
		Map<String,Object> data = new HashMap<>(16);
		String requestUrl = request.getServletPath();
		requestUrl = requestUrl.substring(0,requestUrl.lastIndexOf("/")+1);
		if(ParametersUtil.isNotNull(id)){
			data.put("id",id);
		}
		if(ParametersUtil.isNotNull(subsetId)){
			data.put("subsetId",subsetId);
		}
		if(ParametersUtil.isNotNull(type)){
			data.put("type",type);
		}
		return new ModelAndView(requestUrl+url , data);
	}

	@RequestMapping(value = {"/", "index.html"},method=RequestMethod.GET)
	public String index(){
		return "index";
	}

	@RequestMapping(value="{url}.htm",method=RequestMethod.GET)
	public String notFound(@PathVariable("url") String url){
		return url;
	}

}
